<template>
    <div>
       <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form :model="queryParams" ref="queryForm" :inline="true" >
            <el-form-item label="二级单位" prop="parentId">
              <treeselect v-model="queryParams.parentId" :options="deptOptions" :default-expand-level="2"
              :show-count="true" placeholder="请选择二级单位"
                size="small" style="width: 500px;" @change="handleQuery"/>
            </el-form-item>
            <el-form-item label="实验室名称">
              <el-input
                v-model="queryParams.orgName"
                placeholder="请输入实验室名称"
                clearable
                size="small"
                style="width:220px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="cyan" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="20">
         <!--用户数据-->
        <el-col :span="12" :xs="24">
        <el-table v-loading="loading" :data="orgList" @row-click="chooseOrg" height="400px" style="overflow-y: auto;">
          <el-table-column label="二级单位"  prop="parentName" :show-overflow-tooltip="true" />
          <el-table-column label="实验室名称"  prop="orgName" :show-overflow-tooltip="true" />
        </el-table>
        </el-col>
        <el-col :span="2">
           <el-button size="small" @click="chooseAllOrg" plain type="text" style="margin-top: 100px;">全选 >></el-button>
           <br/>
           <el-button size="small" @click="unchooseAllOrg" plain type="text" style="margin-top: 30px;">取消 <<</el-button>
        </el-col>
        <!--已经选择的用户数据-->
        <el-col :span="10" :xs="24">
          <el-table :data="choosedOrgList" @row-click="unchooseOrg"  height="400px" style="overflow-y: auto;">
            <el-table-column label="二级单位"  prop="parentName" :show-overflow-tooltip="true" />
            <el-table-column label="实验室名称"  prop="orgName" :show-overflow-tooltip="true" />
          </el-table>
        </el-col>
    </el-row>
    <el-row>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
     <el-button icon="el-icon-check"type="primary" @click="submitChoose" size="small">确定</el-button>
     <el-button icon="el-icon-close" @click="cancel" size="small">取 消</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrgnization,treeselect } from "@/api/labdata/orgnization";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "ChooseOrgan",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: [],
      // 总条数
      total: 0,
      // 用户表格数据
      orgList: null,
      choosedOrgList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgFunctionOpt: 4,
        orgName: undefined
      },
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    submitChoose(){
      //父类注册组件，调用方式
      //<UserChoose @handlechooseOrg="getUser" ></UserChoose>
      //调用注册的父类方法
      //把已经选择的用户信息 choosedOrgList 传递给父类 handleChooseUser 方法
      this.$emit("chooseOrgs", this.choosedOrgList);
      this.open= false;
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect({orgFunctionOpt:3}).then(response => {
        this.deptOptions = response.data;
      });
    },
    chooseOrg(row){
      for(var i=0;i<this.orgList.length;i++){
        if(this.orgList[i].orgId==row.orgId){
            var choosed = false;
            for(var j=0;j<this.choosedOrgList.length;j++){
              if(this.choosedOrgList[j].orgId == row.orgId){
                choosed = true;
              }
            }
            if(!choosed){
              this.choosedOrgList.push(this.orgList[i]);
            }
        }
      }
      for(var j=0;j<this.choosedOrgList.length;j++){
         for(var i=0;i<this.orgList.length;i++){
           if(this.orgList[i].orgId == this.choosedOrgList[j].orgId){
             this.orgList.splice(i,1);
             i--;
           }
         }
      }
  },
  chooseAllOrg(){
    for(var i=0;i<this.orgList.length;i++){
          var choosed = false;
          for(var j=0;j<this.choosedOrgList.length;j++){
            if(this.choosedOrgList[j].orgId == this.orgList[i].orgId){
              choosed = true;
            }
          }
          if(!choosed){
            this.choosedOrgList.push(this.orgList[i]);
          }
    }
    this.orgList = [];
  },
  unchooseOrg(row){
    for(var j=0;j<this.choosedOrgList.length;j++){
      if(this.choosedOrgList[j].orgId == row.orgId){
        this.orgList.push(this.choosedOrgList[j]);
        this.choosedOrgList.splice(j,1);
      }
    }
  },
  unchooseAllOrg(){
    for(var j=0;j<this.choosedOrgList.length;j++){
        this.orgList.push(this.choosedOrgList[j]);
    }
    this.choosedOrgList = [];
  },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listOrgnization(this.queryParams).then(response => {
          this.orgList = response.rows;
          this.removeChoosedOrgList()
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    removeChoosedOrgList() {
      for(var j=0;j<this.choosedOrgList.length;j++){
        for(var i=0;i<this.orgList.length;i++){
          if(this.orgList[i].orgId == this.choosedOrgList[j].orgId){
            this.orgList.splice(i,1);
            i--;
          }
        }
      }
    },
    /** 新增按钮操作 */
    handleAdd(orgList) {
      this.open = true;
      this.title = "选择机构";
      if (orgList) {
        this.choosedOrgList = [];
        for(var i=0;i<orgList.length;i++){
          this.choosedOrgList.push(orgList[i]);
        }
      } else {
        this.choosedOrgList = []
      }
      this.resetQuery()
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        orgFunctionOpt: 4,
        orgName: undefined
      }
      this.handleQuery();
    },

  }
};
</script>
<style lang="scss" scoped>
//表格鼠标悬停的样式（背景颜色）
/deep/ {
  .el-table {
    tbody tr {
      &:hover {
        td {
          cursor: pointer;
        }
      }
    }
  }
}
/deep/.el-pagination__jump{
  margin-left: 0px;
}
/deep/ .el-pagination__sizes{
  margin: 0 0px 0 0;
}
/deep/ .el-form-item {
    margin-bottom: 10px;
}
/deep/ .el-dialog__body {
    padding: 0px 20px;
}
</style>
